package com.zls.service.impl;

import com.zls.mapper.ItemMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.Item;
import com.zls.service.ItemService;
import com.zls.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<Item> page = itemMapper.findItem((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = itemMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<Item> findAll() {
        return itemMapper.findAll();
    }

    @Override
    @Transactional
    public int addItem(Item item) {
        item.setUpdatetime(new Date());
        item.setIid(UUIDUtils.uuid());
        int i = itemMapper.addItem(item);
        return i;
    }

    @Override
    public Item findByIid(String iid) {
        return itemMapper.findByIid(iid);
    }

    @Override
    @Transactional
    public int editItem(Item item) {
        item.setUpdatetime(new Date());
        int i = itemMapper.updateItem(item);
        return i;
    }

    @Override
    @Transactional
    public int deleteItem(String iid) {
        int i = itemMapper.deleteItem(iid);
        return i;
    }

    @Override
    public List<Item> findBySuid(String suid) {
        return itemMapper.findBySuid(suid);
    }
}
